import { defineStore } from "pinia";
// import { ref, reactive } from "vue";
import { refreshToken } from "../axios/Api/xsp_api";
export const useLoginStore = defineStore("login", {
  actions: {
    async Login(userInfo) {
      localStorage["accessToken"] = userInfo.accessToken;
      localStorage["refreshToken"] = userInfo.refreshToken;
    },
    async RefreshToken() {
      // 使用当前的Refresh Token发起刷新请求
      const {
        data: { accessToken, code },
      } = await refreshToken({
        refreshToken: localStorage.getItem("refreshToken"),
      });
      if (code === 200) {
        localStorage["accessToken"] = accessToken;
        console.log("token刷新成功");
      } else {
        // 处理错误响应，如清除存储的tokens并提示用户重新登录
        console.log("刷新token失败");
        localStorage.removeItem("accessToken");
        localStorage.removeItem("refreshToken");
      }
    },
  },
});
